

define(['common/controllers', 'domReady'],
    function (controllers, domReady) {
        controllers.controller('taskCtrl',function ($scope,$location,$state,$cacheFactory,taskService,validation) {
            $scope.params = {
                taskType : 1,
                size:5,
                page:1
            }
            $scope.currentPage = 1;
            $scope.taskList = [];
            $scope.point=[];
            $scope.pages = [];
            $scope.taskItemType = [{typeId:1,typeName:'下载安装试用3分钟'},
                                     {typeId:2,typeName:'分享朋友圈'},
                                     {typeId:3,typeName:'打开网页'},
                                     {typeId:4,typeName:'打开试用三分钟'}]
            $scope.taskType = [
                {typeId:1,typeName:'普通任务'},
                {typeId:2,typeName:'红包任务'}

            ]

            var taskItem = {
                taskItemName:'',
                taskItemtype:'',
                taskItemMoney:'',
                describe:'',
                taskItemLink:'',
                openTimeInterval:'',
                ranking:0,
                packetName:''
            }
            var load = function(params){
                params.page =$scope.currentPage;
                taskService.getTaskList(params,function(err,data){
                    if(err){
                        console.log(err);
                        return false;
                    }
                    $scope.taskList = data.results;

                    $scope.count = data.counts;
                    console.log(data);
                    //分页后的订单
                    $scope.numPages = data.counts>0?Math.ceil(data.counts / params.size):1;
                    $scope.pageStart = data.counts>0?($scope.currentPage - 1) * params.size + 1:0;
                    $scope.pageEnd = params.size * $scope.currentPage > data.counts ? data.counts : $scope.currentPage * params.size;




                })
            }

            load($scope.params);

            $scope.changeTaskStatus = function(taskId,taskStatus){
                taskService.changeTaskStatus(taskId,taskStatus,function(err,data){
                    if(err){
                        return false;
                    }
                    load($scope.params);
                })
            }

            $scope.delete = function(taskId){
                taskService.changeTaskState(taskId,function(err,data){
                    if(err){
                        return false;
                    }
                    $scope.count--;
                    if($scope.count != 0){
                        if(($scope.currentPage-1)*$scope.params.size>=$scope.count){
                            $scope.currentPage--;
                        }
                    }else{
                        $scope.currentPage = 1;
                    }

                    load($scope.params);
                })
            }

            // 翻页
            $scope.onSelectPage = function (page) {
                $scope.currentPage = page;
                //$scope.params.page = page;

                load($scope.params);
            };

            $scope.judge=function(page,index){
                if(Math.abs($scope.currentPage - page)==5&&page!=1&&page!=$scope.numPages){
                    $scope.point[index]=true;
                }else{
                    $scope.point[index]=false;
                }

                if(Math.abs($scope.currentPage - page)<=5||page==$scope.numPages||page==1){
                    return true;
                }else{
                    return false;
                }
            }

            //添加（创建）任务
            $scope.addTask=function(){
                $scope.task={
                    taskName:'',
                    icon:'',
                    taskMoney:'',
                    taskQuantity:'',
                    taskItemsNumber:'',
                    taskType:1,
                    taskItems:[{
                        taskItemName:'',
                        taskItemtype:'',
                        taskItemMoney:'',
                        describe:'',
                        taskItemLink:'',
                        openTimeInterval:'',
                        ranking:0,
                        packetName:''
                    }]
                }


                $('#addTaskModal').modal();
            }

            $scope.addTaskItem = function(){
               // $scope.task.taskItems.length++;
                $scope.task.taskItems.push({
                    taskItemName:'',
                    taskItemType:'',
                    taskItemMoney:'',
                    describe:'',
                    taskItemLink:'',
                    openTimeInterval:'',
                    ranking:0,
                    packetName:''
                });

            }

            $scope.deleteTaskItem = function(index){
                if($scope.task.taskItems.length > 1){
                    $scope.task.taskItems.splice(index,1);
                }else{
                    alert('请确保有一个子任务!!!');
                }

            }

            //先验证数据的有效性,再创建任务
            $scope.createTask = function(){

                //子任务的金额总和
                var taskItemMoneySum = 0;
                //验证任务名
                if(!validation.checkTaskName($scope.task.taskName)){
                    return false;
                }

                //验证图片
                if(!validation.checkImg($scope.task.icon)){
                    return false;
                }

                //验证金额
                if(!validation.checkMoney($scope.task.taskMoney)){
                    return false;
                }


                //验证任务数量
                if(!validation.checkTaskQuantity($scope.task.taskQuantity)){
                    return false;
                }

                //验证子任务数
                if(!validation.checkTaskItemNumber($scope.task.taskItemsNumber)){
                    return false;
                }

                //验证子任务
                for(i=0;i<$scope.task.taskItems.length;i++){
                    //验证子任务名
                    if(!validation.checkTaskName($scope.task.taskItems[i].taskItemName)){
                        return false;
                    }

                    //验证任务类型
                    if(!validation.checkTaskType($scope.task.taskItems[i].taskItemType)){
                        return false;
                    }

                    //验证子任务金额
                    if(!validation.checkMoney($scope.task.taskItems[i].taskItemMoney)){
                        return false;
                    }

                    //验证描述内容，可以为空，不能出现中文的'；'
                    if(!validation.checkTaskItemDescribe($scope.task.taskItems[i].describe)){
                        return false;
                    }

                    //验证时间间隔
                    if(!validation.checkTimeInterval($scope.task.taskItems[i].openTimeInterval)){
                        return false;
                    }

                 taskItemMoneySum += parseInt($scope.task.taskItems[i].taskItemMoney*100);
                }
                console.log(taskItemMoneySum);
                console.log($scope.task.taskMoney*100);
                //验证金额与子任务数是否一致
                if($scope.task.taskItemsNumber != $scope.task.taskItems.length){
                    alert('请确保子任务数量一致');
                    return false;
                }

                if(parseInt($scope.task.taskMoney*100) != taskItemMoneySum){
                    alert('请确保子任务金额与总任务金额一致');
                    return false;
                }




                taskService.createTask($scope.task,function(err,data){
                    if(err){
                        console.log(err);
                        location.reload();
                    }
                    location.reload();
                })

            }

            //编辑任务
            $scope.editor = function(taskId){
                //根据ID查询当前任务
                taskService.getTaskDetail(taskId,function(err,data){

                    $scope.task = data;
                })
                $('#updateTaskModal').modal();
            }


            //确认修改任务,暂时只修改总任务信息
            $scope.updateTask = function(task){
                taskService.updateTask(task,function(err,data){
                    if(err){
                        alert('修改任务信息失败');
                        location.reload();
                    }

                    location.reload();
                })
            }


        })

        controllers.controller('taskDetailCtrl',function ($scope,$stateParams,$location,$state,$cacheFactory,taskService) {
            var taskId = $stateParams.task_id;

            $scope.taskDetail = {};

            taskService.getTaskDetail(taskId,function(err,data){
                if(err){
                    return err;
                }
                $scope.taskDetail = data;
                if($scope.taskDetail.task_type == 1){
                    $scope.taskDetail.type = '普通任务';
                }else if($scope.taskDetail.task_type == 2){
                    $scope.taskDetail.type = '红包任务';
                }
                if($scope.taskDetail.childTask.length > 0){
                    $scope.taskDetail.childTask.forEach(function(item){
                        if(item.task_item_type == 1){
                            item.type = '下载安装试用3分钟';
                        }else if(item.task_item_type == 2){
                            item.type = '分享朋友圈';
                        }else if(item.task_item_type == 3){
                            item.type = '打开网页';
                        }else if(item.task_item_type == 4){
                            item.type = '打开试用三分钟';
                        }
                    })
                }

            })



        })

    });

